﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Magazines;

namespace VK.Casemeet.Web.Admin.Magazines
{
    public partial class MagazineEdit : System.Web.UI.Page
    {
        public int magazineID = 0;
        public string documentUrl = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="电子报列表",Url="/Admin/Magazines/MagazineList.aspx"},
                new MapPath{ Text="编辑",Url="#"}
            };

            magazineID = Util.GetQueryInt("MagazineID");

            if (!IsPostBack)
            {
                //GoodsHelper.BindDeprDDL(ddlDepr, OptionType.Select);

                LoadData();
            }
        }

        internal void LoadData()
        {
            if (magazineID != 0)
            {
                MagazineEntity magazine = MagazineBLL.GetMagazine(magazineID);
                txtTitle.Text = magazine.Title;
                documentUrl = magazine.FileUrl;
                txtCreated.Text = Util.FormatDateTime(magazine.Created, DateTimeStyle.ShortDate2);
                txtSort.Text = magazine.Sort.ToString();
                chkIsEnable.Checked = magazine.IsEnable;
                newsPhoto1.defaultImgUrl = magazine.ImgUrl;
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            MagazineEntity magazine = LoadEntity();
            magazine.MagazineID = magazineID;

            if (MagazineBLL.UpdateMagazine(magazine) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected MagazineEntity LoadEntity()
        {
            MagazineEntity magazine = new MagazineEntity();
            magazine.Title = txtTitle.Text;
            magazine.ImgUrl = newsPhoto1.ValueUrl;
            magazine.FileUrl = Util.GetFormString("hidDocumentUrl");
            magazine.Created = Util.ConverDateTime(txtCreated.Text);
            magazine.Sort = int.Parse(txtSort.Text);
            magazine.IsEnable = chkIsEnable.Checked;

            return magazine;
        }
    }
}